package com.mindskip.xzs.listener;

import com.mindskip.xzs.domain.dto.student.exampaper.ExamPaperAnswerInfo;
import com.mindskip.xzs.domain.entity.ExamPaper;
import com.mindskip.xzs.domain.entity.ExamPaperAnswer;
import com.mindskip.xzs.domain.entity.ExamPaperQuestionCustomerAnswer;
import com.mindskip.xzs.domain.entity.TextContent;
import com.mindskip.xzs.domain.enums.ExamPaperTypeEnum;
import com.mindskip.xzs.domain.enums.QuestionTypeEnum;
import com.mindskip.xzs.event.CalculateExamPaperAnswerCompleteEvent;
import com.mindskip.xzs.service.ExamPaperAnswerService;
import com.mindskip.xzs.service.ExamPaperQuestionCustomerAnswerService;
import com.mindskip.xzs.service.TaskExamCustomerAnswerService;
import com.mindskip.xzs.service.TextContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class CalculateExamPaperAnswerListener implements ApplicationListener<CalculateExamPaperAnswerCompleteEvent> {

    private final ExamPaperAnswerService examPaperAnswerService;
    private final ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService;
    private final TextContentService textContentService;
    private final TaskExamCustomerAnswerService examCustomerAnswerService;

    /**
     * Instantiates a new Calculate exam paper answer listener.
     */
    @Autowired
    public CalculateExamPaperAnswerListener(ExamPaperAnswerService examPaperAnswerService, ExamPaperQuestionCustomerAnswerService examPaperQuestionCustomerAnswerService, TextContentService textContentService, TaskExamCustomerAnswerService examCustomerAnswerService) {
        this.examPaperAnswerService = examPaperAnswerService;
        this.examPaperQuestionCustomerAnswerService = examPaperQuestionCustomerAnswerService;
        this.textContentService = textContentService;
        this.examCustomerAnswerService = examCustomerAnswerService;
    }

    @Override
    @Transactional
    public void onApplicationEvent(CalculateExamPaperAnswerCompleteEvent calculateExamPaperAnswerCompleteEvent) {
        Date now = new Date();
        ExamPaperAnswerInfo examPaperAnswerInfo = (ExamPaperAnswerInfo) calculateExamPaperAnswerCompleteEvent.getSource();
        ExamPaper examPaper = examPaperAnswerInfo.getExamPaper();
        ExamPaperAnswer examPaperAnswer = examPaperAnswerInfo.getExamPaperAnswer();
        List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers = examPaperAnswerInfo.getExamPaperQuestionCustomerAnswers();

        // 用户提交的试卷答案进行入库
        int i1 = examPaperAnswerService.insertByFilter(examPaperAnswer);
        if (i1 <= 0) {
            throw new RuntimeException("用户提交的试卷答案入库失败");
        }

        // 如果题目是简答题或者填空题
        examPaperQuestionCustomerAnswers.stream()
                .filter(a -> QuestionTypeEnum.needSaveTextContent(a.getQuestionType()))
                .forEach(d -> {
                    TextContent textContent = new TextContent(d.getAnswer(), now);
                    // 将用户提交的该题目答案存入到 t_tet_content 表中
                    textContentService.insertByFilter(textContent);

                    // 给简答题或填空题设置属性 textContentId
                    d.setTextContentId(textContent.getId());
                    // 给简答题或填空题属性 answer 设置为null
                    d.setAnswer(null);
                });

        // 给每个 ExamPaperQuestionCustomerAnswer 设置属性 examPaperAnswerId
        examPaperQuestionCustomerAnswers.forEach(d -> {
            d.setExamPaperAnswerId(examPaperAnswer.getId());
        });

        // 批量将用户提交的试卷题目答案进行入库
        int i = examPaperQuestionCustomerAnswerService.insertList(examPaperQuestionCustomerAnswers);
        if (i != examPaperQuestionCustomerAnswers.size()) {
            throw new RuntimeException("试卷提交题目答案部分入库失败");
        }

        if (ExamPaperTypeEnum.fromCode(examPaper.getPaperType()) == ExamPaperTypeEnum.Task) {
            // 如果用户提交的试卷是任务试卷，给其插入或修改对应的 t_task_exam_customer_answer
            examCustomerAnswerService.insertOrUpdate(examPaper, examPaperAnswer, now);
        }
    }
}
